package com.mySampleApplication.client.codeRule;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mySampleApplication.client.components.SDataSource;
import com.mySampleApplication.client.dmp.DmpDto;
import com.mySampleApplication.client.dmp.DmpService;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.*;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickEvent;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.smartgwt.client.widgets.tree.TreeNode;

public class dataObjectwindow {
    public static Window getShowWindow(int width, int height, String title,ListGridRecord record,ListGrid listGrid,TreeNode[] employeeData,TextItem modelItem,TextItem modelItemId){
        final Window winModal = new Window();
        winModal.setWidth(width);
        winModal.setHeight(height);
        winModal.setTitle(title);
        winModal.setShowMinimizeButton(false);
        winModal.setIsModal(true);
        winModal.setShowModalMask(true);
        winModal.setCanDragReposition(true);
        winModal.setCanDragResize(true);
        winModal.centerInPage();
        winModal.addCloseClickHandler(new CloseClickHandler() {
            public void onCloseClick(CloseClickEvent event) {
                winModal.destroy();
            }
        });

        // 创建一个垂直布局容器
        VLayout sjdxlayout = new VLayout(10); // 参数15为布局成员之间的间距
        sjdxlayout.setWidth100();
        sjdxlayout.setHeight100();
        Map<String, Object> sjdxlistMap =dataObjecForm.getListGrid(listGrid,record,winModal,modelItem, modelItemId);
        ListGrid sjdxlistGrid = (ListGrid) sjdxlistMap.get("listGrid");
        List<ListGridRecord> recordList = new ArrayList<>();
        DmpService.App.getInstance().selectObjectList(new AsyncCallback<DmpDto>() {
            public void onSuccess(DmpDto dmpDto) {
                List<Map<String, String>> keyValueMap = dmpDto.getKeyValueListMap();
                for (Map<String, String> data : keyValueMap) {
                    ListGridRecord record = new ListGridRecord();
                    data.forEach((k, v) -> {
                        record.setAttribute(k, v);
                    });
                    // 设置其他需要的属性
                    recordList.add(record);
                }
                sjdxlistGrid.setData(recordList.toArray(new ListGridRecord[0]));
            }

            public void onFailure(Throwable caught) {
                SC.warn("获取数据失败！");
            }
        });

        // 加载数据
        sjdxlistGrid.fetchData();
        // 添加列表网格到布局容器中
        sjdxlayout.addMember(sjdxlistGrid);
        sjdxlayout.addMember((HLayout) sjdxlistMap.get("buttonLayout"));

        Tree employeeTree = new Tree();
        employeeTree.setModelType(TreeModelType.PARENT);
        employeeTree.setRootValue(1);
        employeeTree.setNameProperty("Name");
        employeeTree.setIdField("EmployeeId");
        employeeTree.setParentIdField("ReportsTo");
        employeeTree.setData(employeeData);

        final TreeGrid employeeTreeGrid = new TreeGrid();
        employeeTreeGrid.setWidth(200);
        employeeTreeGrid.setHeight(418);
        employeeTreeGrid.setClosedIconSuffix("");
        employeeTreeGrid.setData(employeeTree);

        employeeTreeGrid.addCellClickHandler(new CellClickHandler() {
            @Override
            public void onCellClick(CellClickEvent cellClickEvent) {

                Map<String, String> mapPage = new HashMap<>();
                mapPage.put("category_id", employeeTreeGrid.getRecord(cellClickEvent.getRowNum()).getAttribute("EmployeeId"));
                List<ListGridRecord> datarecordList = new ArrayList<>();
                DmpService.App.getInstance().selectObjectListBy(mapPage,new AsyncCallback<DmpDto>() {
                    public void onSuccess(DmpDto dmpDto) {
                        List<Map<String, String>> keyValueMap = dmpDto.getKeyValueListMap();
                        for (Map<String, String> data : keyValueMap) {
                            ListGridRecord datarecord = new ListGridRecord();
                            data.forEach((k, v) -> {
                                datarecord.setAttribute(k, v);
                            });
                            // 设置其他需要的属性
                            datarecordList.add(datarecord);
                        }
                        sjdxlistGrid.setData(datarecordList.toArray(new ListGridRecord[0]));
                    }
                    public void onFailure(Throwable caught) {
                        SC.warn("获取数据失败！");
                    }
                });
            }
        });

        HLayout buttonLayout = new HLayout();
        buttonLayout.setMembersMargin(10);
        buttonLayout.setAlign(Alignment.RIGHT);
        buttonLayout.setWidth100();
        buttonLayout.setHeight(30);
        buttonLayout.setPadding(5);
        buttonLayout.setLayoutAlign(VerticalAlignment.BOTTOM);

        HLayout layout = new HLayout();
        layout.setPadding(10);
        layout.setHeight(40);
        DataSource dataSourceA = SDataSource.getDataSource();

        DynamicForm filterForm =getFilterForm(dataSourceA,sjdxlistGrid);
        filterForm.setWidth(340);
        HLayout hLayout =getSerchButton(sjdxlistGrid, filterForm);
        layout.addMembers(filterForm,hLayout);

        HLayout navLayout = new HLayout();
        navLayout.setHeight(450);
        navLayout.setMembers(employeeTreeGrid, sjdxlayout);

        winModal.addItem(layout);
        winModal.addItem(navLayout);
        winModal.addItem(buttonLayout);
        return winModal;
    }

    public static DynamicForm getFilterForm(DataSource dataSource, ListGrid listGrid){
        final DynamicForm filterForm = new DynamicForm();
        filterForm.setNumCols(3);
        filterForm.setDataSource(dataSource);
        filterForm.setAutoFocus(false);
        TextItem nameItem = new TextItem("中文名称");
        filterForm.setFields(nameItem);
        return filterForm;
    }

    public static HLayout getSerchButton(ListGrid listGrid, DynamicForm filterForm){
        HLayout hLayout = new HLayout(10);
        hLayout.setHeight(22);
        VLayout layout = new VLayout(); // 或者使用 HLayout
        Button filterButton = new Button("search");
        filterButton.setTitle("搜索");
        filterButton.setWidth(60);
        filterButton.setShowRollOver(true);
        filterButton.setShowDown(true);
//        filterButton.setIcon("[SKIN]/actions/view_Disabled.png"); // 设置图标路径
        filterButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Map<String, String> mapPage = new HashMap<>();
                mapPage.put("name_cn", filterForm.getValueAsString("中文名称"));

                List<ListGridRecord> datarecordList = new ArrayList<>();
                DmpService.App.getInstance().selectObjectListBy(mapPage,new AsyncCallback<DmpDto>() {
                    public void onSuccess(DmpDto dmpDto) {
                        List<Map<String, String>> keyValueMap = dmpDto.getKeyValueListMap();
                        for (Map<String, String> data : keyValueMap) {
                            ListGridRecord datarecord = new ListGridRecord();
                            data.forEach((k, v) -> {
                                datarecord.setAttribute(k, v);
                            });
                            // 设置其他需要的属性
                            datarecordList.add(datarecord);
                        }
                        listGrid.setData(datarecordList.toArray(new ListGridRecord[0]));
                    }
                    public void onFailure(Throwable caught) {
                        SC.warn("获取数据失败！");
                    }
                });
            }
        });
        filterButton.setAutoFit(true);

        IButton clearButton = new IButton("Clear");
        clearButton.setTitle("重置");
        clearButton.setIcon("[SKIN]/actions/refresh.png"); // 设置图标路径
        clearButton.setAutoFit(true);
        clearButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                List<ListGridRecord> recordList = new ArrayList<>();
                DmpService.App.getInstance().selectObjectList(new AsyncCallback<DmpDto>() {
                    public void onSuccess(DmpDto dmpDto) {
                        List<Map<String, String>> keyValueMap = dmpDto.getKeyValueListMap();
                        for (Map<String, String> data : keyValueMap) {
                            ListGridRecord record = new ListGridRecord();
                            data.forEach((k, v) -> {
                                record.setAttribute(k, v);
                            });
                            // 设置其他需要的属性
                            recordList.add(record);
                        }
                        listGrid.setData(recordList.toArray(new ListGridRecord[0]));
                    }

                    public void onFailure(Throwable caught) {
                        SC.warn("获取数据失败！");
                    }
                });
                filterForm.clearValues();
            }
        });

        layout.setDefaultLayoutAlign(Alignment.RIGHT);
        hLayout.addMember(filterButton);
//        hLayout.addMember(clearButton);
        hLayout.addMember(layout);
        return hLayout;
    }

}
